<?php
/**
 * Created by PhpStorm.
 * User: huangpeng3
 * Date: 2017/12/27
 * Time: 下午7:24
 * 市场操作
 */
namespace Home\Controller;
use Think\Controller;
use Self\Http\RequestTool;


class MarketController extends Controller
{


    function __construct()
    {
        $this->config = C();
    }

    /**
     * 获取某一交易所返回的行情数据
     * @param $eid  交易所主键
     * @param $market   币种市场
     */
    public function ticker($eid, $currency)
    {
        switch ($eid) {
            //zb
            case 1:
                $this->getTickerZb($eid, $currency);
            //火币
            case 2:
                $this->getTickerHuobi($eid, $currency);
            //okex
            case 3:
                $this->getTickerOkex($eid, $currency);
        }
    }

    /**
     *获取zb的市场行情接口
     * @param $currency
     */
    public function getTickerZb($eid, $currency)
    {
        $exchangeInfo = M('exchanges')->field('id, label')->where('id=' . $eid)->select();
        if (isset($exchangeInfo[0]['label'])) {
            $apiPre = $exchangeInfo[0]['label'];
            if (isset($this->config['URL_ZB']['api_' . $apiPre . '_ticker'])) {
                $url = $this->config['URL_ZB']['api_' . $apiPre . '_ticker'];
                $param = array();
                if (isset($this->config['PARAM_ZB']['api_' . $apiPre . '_ticker'])) {
                    $arr = $this->config['PARAM_ZB']['api_' . $apiPre . '_ticker'];
                    foreach ($arr as $v) {
                        if ($v == 'market') {
                            $param[$v] = $currency;
                        }
                    }
                }
                $rst = RequestTool::get($url, $param);
                var_dump($rst);
            }
        }
    }

    /**
     *获取火币的市场行情接口
     * @param $eid
     * @param $currency
     */
    public function getTickerHuobi($eid, $currency, $period = '1min', $size = 150)
    {
        $exchangeInfo = M('exchanges')->field('id, label')->where('id=' . $eid)->select();
        if (isset($exchangeInfo[0]['label'])) {
            $apiPre = $exchangeInfo[0]['label'];
            if (isset($this->config['URL_HUOBI']['api_' . $apiPre . '_ticker'])) {
                $url = $this->config['URL_HUOBI']['api_' . $apiPre . '_ticker'];
                $param = array();
                if (isset($this->config['PARAM_HUOBI']['api_' . $apiPre . '_ticker'])) {
                    $arr = $this->config['PARAM_HUOBI']['api_' . $apiPre . '_ticker'];
                    foreach ($arr as $v) {
                        if ($v == 'symbol') {
                            $param[$v] = $currency;
                        } elseif ($v == 'period') {
                            $param[$v] = $period;
                        } elseif ($v == 'size') {
                            $param[$v] = $size;
                        }
                    }
                }
                $rst = RequestTool::get($url, $param);
                var_dump($rst);
            }
        }
    }

    /**
     * 获取okex的市场行情接口
     * @param $eid
     * @param $currency
     */
    public function getTickerOkex($eid, $currency, $contract_type = 'this_week')
    {
        $exchangeInfo = M('exchanges')->field('id, label')->where('id=' . $eid)->select();
        if (isset($exchangeInfo[0]['label'])) {
            $apiPre = $exchangeInfo[0]['label'];
            if (isset($this->config['URL_OKEX']['api_' . $apiPre . '_ticker'])) {
                $url = $this->config['URL_OKEX']['api_' . $apiPre . '_ticker'];
                $param = array();
                if (isset($this->config['PARAM_OKEX']['api_' . $apiPre . '_ticker'])) {
                    $arr = $this->config['PARAM_OKEX']['api_' . $apiPre . '_ticker'];
                    foreach ($arr as $v) {
                        if ($v == 'symbol') {
                            $param[$v] = $currency;
                        } elseif ($v == 'contract_type') {
                            $param[$v] = $contract_type;
                        }
                    }
                }
                $rst = RequestTool::get($url, $param);
                var_dump($rst);
            }
        }
    }

}